package com.southwind.service.impl;

import com.southwind.entity.Building;
import com.southwind.mapper.BuildingMapper;
import com.southwind.mapper.DormitoryMapper;
import com.southwind.mapper.StudentMapper;
import com.southwind.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuildingServiceImpl implements BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List<Building> list() {
        return this.buildingMapper.list();
    }

    @Override
    public List<Building> search(String key, String value) {
        if(value.equals("")) return this.buildingMapper.list();
        List<Building> list = null;
        switch (key)
        {
            case "name":
                list=this.buildingMapper.searchByName(value);
                break;
            case "introduction":
                list=this.buildingMapper.searchByIntroduction(value);
                break;
        }
        return list;
    }

    @Override
    public void save(Building building) {
        try {
            this.buildingMapper.save(building);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(Building building) {
        try {
            this.buildingMapper.update(building);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
try {
        //找到building包含的所有宿舍
        List<Integer> dormitoryIdList = this.dormitoryMapper.findDormitoryIdByBuildingId(id);
        for (Integer dormitoryId : dormitoryIdList) {

        //找到宿舍包含的全部学生
            List<Integer> studentIdList = this.studentMapper.findStudentIdByDormitoryId(dormitoryId);
            for (Integer studentId : studentIdList) {

                //学生调换宿舍
                Integer availableDormitoryId = this.dormitoryMapper.findAvailableDormitoryId();
                    this.studentMapper.resetDormitoryId(studentId,availableDormitoryId);
                    this.dormitoryMapper.subAvailable(availableDormitoryId);
            }

            //删除宿舍
            this.dormitoryMapper.delete(dormitoryId);
        }
        //删除楼
        this.buildingMapper.delete(id);
        } catch (Exception e) {
    e.printStackTrace();
}
    }

}
